/**
 * 
 */
package com.googlecode.pcapj;

import java.util.LinkedHashMap;

/**
 * Represents the IPv4 protocol in the Network Layer.
 */
public class IPv4 implements NetworkLayer<IPv4Packet>{
	/**
	 * Defines protocol numbers.
	 */
	public static class Protocol {
		/**
		 * UDP 
		 */
		public static final byte UDP = (byte)0x11;
		/**
		 * TCP
		 */
		public static final byte TCP = (byte)0x06;
	}
	
	private LinkedHashMap<Byte, TransportLayer<? extends Packet>> transportLayers = new LinkedHashMap<Byte, TransportLayer<? extends Packet>>();
	
	/* (non-Javadoc)
	 * @see com.googlecode.pcapj.PacketFactory#newPacket(byte[], int, int)
	 */
	public IPv4Packet newPacket(byte[] buffer, int offset, int length) {
		IPv4PacketImpl packet = new IPv4PacketImpl(buffer, offset, length);
		PacketFactory<? extends Packet> packetFactory = transportLayers.get(packet.getProtocol());
		if(packetFactory == null){
			packetFactory = PacketFactoryImpl.DEFAULT_FACTORY;
		}
		packet.setPacketFactory(packetFactory);
		return packet;
	}

	/**
	 * Gets the editable mappings of a protocol number and a network layer protocol.
	 * @return the editable mappings of a protocol number and a network layer protocol.
	 */
	public LinkedHashMap<Byte, TransportLayer<? extends Packet>> getTransportLayers() {
		return transportLayers;
	}
	
}
